/**
 * 
 */
var callback=0;
var totalNum=1;

var s_id;
function StudentList(pageNo){
	$.ajax({
		url:basePath+'/student/stuList',
		data:{'pageNum':pageNo,'pageSize':10},
		dataType:'json',
		type:'post',
		success:function(data)
		{
			 $("#Stbody").empty();
		    	
			 var count=totalNum;
			 totalNum=data.json.count/10;
			 if(count!=totalNum)
			     initPagination();
		
			for(var i=0;i<data.json.StudentList.length;i++){
				var sex;
				(data.json.StudentList[i].sSex==1)?sex="男":sex="女";
		          $("#Stbody").append('<tr><td>'+data.json.StudentList[i].username+'</td><td>'+sex+'</td>'
		        		 +'<td>'+data.json.StudentList[i].sBeginyear+'</td>'
		        		 +'<td>'+data.json.StudentList[i].sBegingrade+'</td><td>'+data.json.StudentList[i].sParentname+'</td>'
		        		 +'<td>'+data.json.StudentList[i].sParenttel+'</td><td>  <div class="hidden-sm hidden-xs action-buttons">'
                         +'<a class="green" href="#" > <a href="#modal-table" role="button" class="green" data-toggle="modal" '
                         +' onclick="putdetail(\''+data.json.StudentList[i].sid+'\',\''+data.json.StudentList[i].sBegingrade+'\',\''+data.json.StudentList[i].sParentname+'\', \''+data.json.StudentList[i].sParenttel+'\')">'
                         +  '<i class="ace-icon fa fa-pencil bigger-130"></i></a> </a>  <a class="red" href="#">'
                         + '<a href="javascript:void(0);" onclick="del(\''+data.json.StudentList[i].sid+'\')"><i class="ace-icon fa fa-trash-o bigger-130"></i></a> </a>'
                         +' </div>  </td></tr>');
		    }
		}
	});
}
	function initPagination() {
		//var num_entries = $("#hiddenresult div.result").length;
		// 创建分页
		$("#Pagination").pagination(totalNum, {
			num_edge_entries: 1, //边缘页数
			num_display_entries: 4, //主体页数
			callback: pageselectCallback,
			items_per_page: 1, //每页显示1项
			prev_text: "前一页",
			next_text: "后一页"
		});
	 }
	
	function pageselectCallback(page_index, jq){
		/*	var new_content = $("#hiddenresult div.result:eq("+page_index+")").clone();
			$("#Searchresult").empty().append(new_content); //装载对应分页的内容
			
	*/	
	         page_index+=1;    
	       
	        	 (callback==1)? callback=0:StudentList(page_index); 
			return false;
		}
	
	
	function  updateStudent()
	{
		
		 var spn=$("#spn").val();
		 var spt=$("#spt").val();
		  var studentUpinfo=s_id+","+spn+","+spt;
		$.ajax({
			url:basePath+'/admin/studentAction!updateSingleStudent',
			type:'post',
			data:{"studentIdList":studentUpinfo},
			dataType:'json',
			success:function()
			{
				toastr.success("更新成功！");
				StudentList(1);
			},
			error:function()
			{
				toastr.error("500:服务器错误！");
			}
			
			
		});
	}
	
	  function del(id){
		 
		        if( confirm("确认删除？"))
		        	deleteStudent(id);
		    } 
	  
	function deleteStudent(stuId)
	{
		$.ajax({
			url:basePath+'/admin/studentAction!DeleteStudent',
			type:'post',
			data:{"studentIdList":stuId},
			dataType:'json',
			success:function()
			{
				toastr.success("删除成功！");
			},
			error:function()
			{
				toastr.error("该学生已报名，不能删除！");
			}
			
			
		});
		
	}
	
	function putdetail(sid,sgrade,spn,spt)
	{
		 s_id=sid;
	    $("#chbody").empty();
	    $("#chbody").append(' <tr id="'+sid+'">'
                +'<td><input class="input-small" id="spn" value="'+spn+'"/></td>'
                +'<td><input class="input-small" id="spt" value="'+spt+'"/></td></tr>');
	}
	StudentList(1);